package com.yswee.mallnavi.model.info;

import com.yswee.framework.data.BaseArrayData;
import com.yswee.framework.data.parser.IParser;
import com.yswee.framework.net.ITaskContext;
import com.yswee.framework.net.http.Callback;
import com.yswee.mallnavi.context.ApiConstant;
import com.yswee.mallnavi.entity.ArrayData;
import com.yswee.mallnavi.entity.info.CategoryEntity;
import com.yswee.mallnavi.entity.info.InfoEntity;
import com.yswee.mallnavi.model.BaseModel;
import com.yswee.mallnavi.parser.BaseGsonParser;
import com.yswee.mallnavi.parser.BaseListGsonParser;
import com.yswee.mallnavi.parser.VoidParser;

import org.json.JSONArray;
import org.json.JSONObject;

public class InfoModel extends BaseModel<Void> {

    public InfoModel(ITaskContext context) {
        super(context);
    }

    @Override
    protected IParser<Void> createParser() {
        return new VoidParser();
    }

    /**
     *
     * @param type 商场介绍=0, 会员介绍=1, 美食介绍=3, 停车介绍=5, 其他=100
     * @param c
     */
    public void getCategoryInfo(int type, Callback<CategoryEntity> c) {
        StringBuilder url = new StringBuilder();
        url.append(ApiConstant.API_INFO_CATEGORY).
            append("?type=").append(type);
        getData(url, new CategoryParser(), c);
    }

    /**
     *
     * @param c
     */
    public void getListInfo(String id, Callback<BaseArrayData<InfoEntity>> c) {
        StringBuilder url = new StringBuilder();
        url.append(ApiConstant.API_INFO_LIST).
                append("?ServiceCategoryId=").append(id).
                append("&skipCount=0&maxResultCount=99");
        getData(url, new ListParser(), c);
    }

    private class CategoryParser extends BaseGsonParser<CategoryEntity> {
        @Override
        public CategoryEntity parseData(String json) {
            try {
                mReponseData = json;
                JSONArray ja = asJSONArray(json);
                if (ja != null && ja.length() > 0) {
                    mIsOk = true;
                    JSONObject jo = ja.getJSONObject(0);
                    return superParseData(jo.toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    private class ListParser extends BaseListGsonParser<InfoEntity> {
        @Override
        public BaseArrayData<InfoEntity> parseData(String json) {
            try {
                mReponseData = json;
                JSONObject jo = asJSONObject(json);
                if (jo != null) {
                    JSONArray ja = jo.optJSONArray("items");
                    ArrayData<InfoEntity> array = new ArrayData<InfoEntity>();
                    for(int i = 0; i < ja.length(); i++) {
                        InfoEntity t = null;
                        try {
                            JSONObject jodata = ja.getJSONObject(i).getJSONObject("serviceItem");
                            t = parseDataT(jodata.toString());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if(t != null) {
                            array.array.add(t);
                        }
                    }
                    mIsOk = true;
                    return array;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}
